<?php
class order extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->tit = '订单';

		$this->modelObj = D('order');
		$this->order_logObj = D('order_log');
		$this->areaObj = D('area');
		$this->cateObj = D('cate');
		// $this->zoneObj = D('company_zone');
		// $this->deviceObj = D('company_device');
		$this->adminObj = D('admin');
		$this->userObj = D('user');

		// $this->area_list = $this->areaObj->getTreeList("status=1 ".$this->cond_base);
		// $this->cate_list = $this->cateObj->getTreeList("status=1 ".$this->cond_base);
		// // $this->zone_list = $this->zoneObj->getTreeList("status=1 ".$this->cond_base);
		// $this->admin_list = $this->adminObj->getTreeList("status=1 ".$this->cond_base);
		// $this->search = array('shifu_id'=>'1','time_s'=>'1');
	}
    
    //列表
	public function index(){
		/*分类切换*/
		$this->tabtit = array_merge(array('全部'),$this->orderStatus);

		$cond = "status=1 ".$this->cond_base;
		// 加入查询条件
		if($this->tab){
			switch($this->tab){
				case '8':
					$cond .=" and order_status=99";
					break;
				default:
					$cond .=" and order_status={$this->tab}";
			}
		}
		// dump($cond);exit;
		//搜索
		if($this->keyword){
			$cond.=" and (title like '%".$this->keyword."%') ";
		}
		
		$result = $this->modelObj->get_list($cond);
        if($result){
			// $this->areas = $this->areaObj->ids($result['list'],'area_id');
			// $this->cates = $this->cateObj->ids($result['list'],'cate_id');
			// // $this->zones = $this->zoneObj->ids($result['list'],'zone_id');
			// $this->admins = $this->adminObj->ids($result['list'],'admin_id');
			// $this->users = $this->userObj->ids($result['list'],'user_id');
			// $this->devices = $this->deviceObj->ids($result['list'],'device_id');

        	$this->list = $result['list'];
			$this->showPage = $result['showPage'];
			$this->total = $result['total'];
		}
		
	}


	//添加
	public function add(){
		if($this->isAjax()){
			$data = $this->postData('data');

			$data['order_status'] = 1;
			$data['admin_id'] = $this->admin['id'];
			$data['company_id'] = $this->company['id'];

			$order_id = $this->modelObj->add($data);

			//日志
			$logdata['company_id'] = $this->company['id'];
			$logdata['admin_id'] = $this->admin['id'];
			$logdata['order_id'] = $order_id;
			$logdata['user_id'] = $data['user_id'];
			$logdata['intro'] = '提交了订单';

			$this->order_logObj->add($logdata);
			$this->renderSuccess('添加成功', "/{$this->routeUrl}/index");
		}
		$this->info = array('status'=>1,'sort'=>100,'type'=>1,'pay_bank'=>1);

		$user_id = arg('user_id');
		if($user_id){
			$this->user = $this->userObj->get_info($user_id);
		}

		$this->display($this->routeUrl."/set.php");
	}

	//编辑
	public function edit(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');
			
			$this->modelObj->edit($id,$data);
			$this->renderSuccess('编辑成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);

		$user_id = arg('user_id');
		if($user_id){
			$this->user = $this->userObj->get_info($user_id);
		}
		$this->display($this->routeUrl."/set.php");
	}

	//审核
	public function act()
	{
		$id = arg('id');
		$status = arg('status');
		$msg = ($status==1)?'显示':'隐藏';
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->act($id,$status);
				$this->renderSuccess($msg.'成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->act($val,1);
					}
					$this->renderSuccess('显示成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}

    //删除
	public function del()
	{
		$id = arg('id');
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->del($id);
				$this->renderSuccess('删除成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->del($val);
					}
					$this->renderSuccess('删除成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
			
		}
	}

	//隐藏
	public function ban()
	{
		$opid = arg('opid');
		if($this->isAjax()){
			if($opid){
				foreach($opid as $val){
					$res = $this->modelObj->act($val,0);
				}
				$this->renderSuccess('隐藏成功');
			}
			$this->renderError('请选择数据');
		}
	}
	
	//排序
	public function sort()
	{
        $sort = arg('sort');
		$result = $this->modelObj->sort($sort);
		$this->renderSuccess('排序成功');
	}


	//订单操作
	public function genjin(){

		if($this->isAjax()){
			$order_id = arg('order_id');
			if($order_id){
				$order = $this->modelObj->findById($order_id);
			}
			$data = $this->postData('data');
			if(!$data['order_status']){
				$this->renderError('请选择订单状态');
			}
			if(!$data['intro']){
				$this->renderError('请输入订单备注');
			}

			$msg = array(
				'1'=>'新订单提交',
				'2'=>'订单已审核',
				'3'=>'订单已提单',
				'4'=>'订单有尾款',
				'5'=>'订单已结清',
				'99'=>'订单已取消',
			);
			$data['intro'] = $msg[$data['order_status']] .' -- '. $data['intro'];


			$data['company_id'] = $this->company['id'];
			$data['admin_id'] = $this->admin['id'];
			$data['order_id'] = $order_id;
			$data['user_id'] = $order['user_id'];

			$this->order_logObj->add($data);

			//更新客户
			$arrBase = array(
				// 'intro'=>$intro,
				'order_status'=>$data['order_status'],
				'up_time'=>date('Y-m-d H:i:s'),
			);
			$this->modelObj->edit($order_id,$arrBase);
			$this->renderSuccess('操作成功');
		}
	}

	// //详情
	// public function detail(){
	// 	$id = arg('id');
	// 	$info = $this->modelObj->get_detail($id);
		
	// 	$this->info = $info;
	// }

	//导出
	public function export(){
		// $id = arg('id');
		// if($this->isAjax()){
		// 	$cond = "status=1".$this->cond_base;

		// 	$data = $this->postData('data');

		// 	$result = $this->orderObj->exportList($cond,$data);

		// 	$this->renderSuccess('导出成功');
		// }
		// $this->info = $this->orderObj->findById($id);
	}

	//导出
	public function export_csv(){

		$data = $this->postData('data');

		$cond = "status=1".$this->cond_base;
		if($data['order_status']){
			switch($data['order_status']){
				case '8':
					$cond .=" and order_status=99";
					break;
				default:
					$cond .=" and order_status={$data['order_status']}";
			}
		}
		if($data['keyword']){
			$cond .=" and (title like '%".$data['keyword']."%') ";
		}
		
		$result = $this->orderObj->exportList($cond,$data);
		
	}

	

	//导入
	public function import(){
		$id = arg('id');
		if($this->isAjax()){
		
			$data = $this->postData('data');
			$csvData = $this->orderObj->getCsvData();
			$res = $this->orderObj->importList($csvData);

			dump($res);exit;

			$this->renderSuccess('导入成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->orderObj->findById($id);
	}

	//导入
	public function deliveryTpl(){
		$this->orderObj->deliveryTpl();
	}

}